package parasoft.centrap.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;

public enum Error {
	undefined_start_date,
	undefined_end_date,
	param_missing,
	param_format_number,
	parse_format_number,
	/** {0} = fileName */
	file_read,
	file_not_found,
	excepted_filename,
	io,
	sql,
	no_broker,
	no_more_connections,
	admin_context,
	library_error,
	operation_types,
	dbmanager_error,
	dbmanager_licence_pass,
	dbmanager_licence_incorrect,
	dbmanager_licence_expired,
	dbmanager_licence_unknown,
	dbmanager_driver,
	no_connection,
	cacher_date_format,
	unknown_driver,
	property_null,
	property_format,
	sql_seq_currval,
	sql_seq_next,
	sql_index,
	unsupported_operation,
	null_connection,
	sql_command,
	sql_command_missing_var,
	sql_command_param_length,
	sql_result_blob,
	sql_blob_unsupported,
	unknown_key,
	field_type_cast,
	field_type_unknown,
	undefined_vat,
	client_date_format,
	client_exist,
	client_card_exist;
	
	
	private static ResourceBundle rb;
	private static final MessageFormat mf = new MessageFormat("");
	private static final Locale locale = Locale.getDefault();
	static{
		refresh();
	}
	private static void refresh(){
		try{
			rb = ResourceBundle.getBundle(System.getProperty("error", "parasoft.centrap.utils.error"), Locale.getDefault());
		}catch(final Exception e){e.printStackTrace();}
	}
	public static String get(final String name){
		if(!locale.equals(Locale.getDefault())){
			refresh();
		}
		if((rb!=null) && rb.containsKey(name)){
			return rb.getString(name);
		}
		return "missing param: "+name;
	}
	@Override
	public String toString() {
		return get(name().replaceAll("_", "."));
	}
	public String format(final Object... params){
		mf.applyPattern(toString());
		return mf.format(params);
	}
	public static NullPointerException nullPointer(){
		return new NullPointerException();
	}
	public ServletException servlet(){
		return new ServletException(toString());
	}
	public ServletException servlet(final Object... params){
		return new ServletException(format(params));
	}

	public NumberFormatException numberFormat(){
		return new NumberFormatException(toString());
	}
	public NumberFormatException numberFormat(final Object... params){
		return new NumberFormatException(format(params));
	}
	public FileNotFoundException fileNotFound(){
		return new FileNotFoundException(toString());
	}
	public FileNotFoundException fileNotFound(final Object... params){
		return new FileNotFoundException(format(params));
	}

	public SQLException sql(){
		return new SQLException(toString());
	}
	public SQLException sql(final Object... params){
		return new SQLException(format(params));
	}
	public IOException io(){
		return new IOException(toString());
	}
	public IOException io(final Object... params){
		return new IOException(format(params));
	}
	public UnsupportedOperationException unsupportedOperation(){
		return new UnsupportedOperationException(toString());
	}
	public UnsupportedOperationException unsupportedOperation(final Object... params){
		return new UnsupportedOperationException(format(params));
	}
	public IndexOutOfBoundsException index(){
		return new IndexOutOfBoundsException(toString());
	}
	public IndexOutOfBoundsException index(final Object... params){
		return new IndexOutOfBoundsException(format(params));
	}

	//DbException
	public static final String e1 = "Brak dostepu do placowek";
	public static final String e2 = "Error: server must be restarted ";
	public static final String e3 = "Blad parsowania daty";
	public static final String e4 = "Can not create connection.";
	public static final String e5 = "Different types of databases selected!";
}
